package org.redmap.controller{
	
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import mx.core.UIComponent;	
	import org.redmap.model.*;
	

	public class MindMapSprite extends UIComponent {

		public var _id:String;
		public var radius:Number;
		public var myWidth:Number;
		public var myHeight:Number;
		public var color:uint;
		public var label:String;
		
		/**
		 * An instance of a purely geometric shape, that is, one that defines
		 * a shape mathematically but not visually.
		 */
		public var geometricShape:IGeometricShape;
		
		/**
		 * Keeps track of the currently selected shape.
		 * This is a static property, so there can only be one GeometricSprite
		 * selected at any given time.
		 */
		public static var selectedSprite:MindMapSprite;
		
		/**
		 * Holds a border rectangle that is shown when this GeometricSprite instance is selected.
		 */
		public var selectionIndicator:MindMapSprite;
		
		public function MindMapSprite(_id:String=null, myWidth:Number=50, myHeight:Number=30, radius:Number=100, color:uint=0xfff000 , label:String=null){
			this._id=_id;
			
			this.myWidth=myWidth;
			this.myHeight=myHeight;
			this.radius=radius;
			this.color=color;
			this.label=label;
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			
			
		}
	
		

		public function drawShape():void{
            // to be overridden in subclasses
		}
		
		public function onMouseDown(evt:MouseEvent):void{
			this.showSelected();
			
			
			
			// limits dragging to the area inside the canvas
			var boundsRect:Rectangle = this.parent.getRect(this.parent);
			trace("this.parent: "+this.parent)
			boundsRect.width -= this.myWidth;
			boundsRect.height -= this.myHeight;
			this.startDrag(false, boundsRect);
			
			//this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}
		
		
		
		
		public function onMouseUp(evt:MouseEvent):void{
			this.stopDrag();

			
		}
		
		private function showSelected():void{
		    if (this.selectionIndicator == null){
		        // draws a red rectangle around the selected shape
		        this.selectionIndicator = new MindMapSprite();
		        this.selectionIndicator.graphics.lineStyle(1.0, 0xFF0000, 1.0);
			    this.selectionIndicator.graphics.drawRect(-1, -1, this.myWidth, this.myHeight);
			    this.addChild(this.selectionIndicator);
		    }
		    else{
		        this.selectionIndicator.visible = true;
		    }
		    
		    if (MindMapSprite.selectedSprite != this){
    		    if (MindMapSprite.selectedSprite != null){
    		        MindMapSprite.selectedSprite.hideSelected();
    		    }
		        MindMapSprite.selectedSprite = this;
		    }
		}
		
		private function hideSelected():void
		{
		    if (this.selectionIndicator != null)
		    {		    
		        this.selectionIndicator.visible = false;
		    }
		}
		
		
		/**
		 * Returns true if this shape's selection rectangle is currently showing.
		 */
		public function isSelected():Boolean
		{
		    return !(this.selectionIndicator == null || this.selectionIndicator.visible == false);
		}
		
		public function okToMove(): Boolean {
			return true;
		}
		
	
		
	}
}